//
//  RegisterViewController.m
//  QuickCure
//
//  Created by haoge on 16/9/10.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "RegisterViewController.h"
#import <SMS_SDK/SMS_SDK.h>
#import <SVProgressHUD.h>

@interface RegisterViewController ()

@property (nonatomic,weak) UITextField *phoneNumTextField;

@property (nonatomic,weak) UITextField *codeTextField;

@property (nonatomic,strong) NSNotification *notification;

@property (nonatomic,weak) UIButton *registerBtn;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"注册";
    
    [self setupUI];
}

- (void)setupUI
{
#pragma mark -- 键盘弹出的通知
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeKeyboard:) name:UIKeyboardWillShowNotification object:nil];
    
    
    //手机号输入框
    UITextField *phoneNumTextField = [[UITextField alloc] init];
    
    self.phoneNumTextField = phoneNumTextField;
    
    phoneNumTextField.borderStyle = UITextBorderStyleRoundedRect;
    
    phoneNumTextField.placeholder = @"请输入用户名";
    
    [self.view addSubview:phoneNumTextField];
    
    [phoneNumTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(50);
        make.leading.equalTo(self.view).offset(20);
        make.trailing.equalTo(self.view).offset(-20);
        make.height.equalTo(@35);
    }];
    
    //验证码输入框
    UITextField *codeTextField = [[UITextField alloc] init];
    
    self.codeTextField = codeTextField;
    
    codeTextField.borderStyle = UITextBorderStyleRoundedRect;
    
    codeTextField.placeholder = @"请输入密码";
    
    [self.view addSubview:codeTextField];
    
    [codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNumTextField.mas_bottom).offset(30);
        make.leading.equalTo(self.view).offset(20);
        make.trailing.equalTo(self.view).offset(-130);
        make.height.equalTo(@35);
    }];
    
    //验证按钮
    UIButton *button = [[UIButton alloc] init];
    
    [button setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    
    [button setTitle:@"获取验证码" forState:UIControlStateNormal];
    
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    
    button.layer.cornerRadius = 5;
    
    button.layer.masksToBounds = YES;
    
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNumTextField.mas_bottom).offset(30);
        make.leading.equalTo(codeTextField.trailing).offset(15);
        make.trailing.equalTo(self.view).offset(-20);
        make.height.equalTo(@35);
    }];
    
    //小button(选择是否同意)
    UIButton *smallBtn = [[UIButton alloc] init];
    
    [smallBtn setBackgroundImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    
    [smallBtn setBackgroundImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    
    [smallBtn addTarget:self action:@selector(smallBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:smallBtn];
    
    [smallBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(codeTextField.mas_bottom).offset(50);
        make.leading.equalTo(self.view).offset(80);
        make.width.height.equalTo(@12);
    }];
    
    //不平等条约
    UILabel *label = [[UILabel alloc] init];
    
    label.text = @"本人不同意并没有阅读";
    
    label.font = [UIFont systemFontOfSize:12];
    
    [self.view addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(smallBtn.trailing).offset(8);
        make.top.equalTo(codeTextField.mas_bottom).offset(50);
        make.centerYWithinMargins.equalTo(smallBtn);
    }];
    
    //条约
    UILabel *paperLabel = [[UILabel alloc] init];
    
    paperLabel.text = @"用户协议";
    
    paperLabel.font = [UIFont systemFontOfSize:13];
    
    paperLabel.textColor = kThemeColor;
    
    [self.view addSubview:paperLabel];
    
    [paperLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(label.trailing).offset(5);
        make.top.equalTo(codeTextField.mas_bottom).offset(50);
        make.centerYWithinMargins.equalTo(label);
    }];
    
    //注册按钮
    UIButton *registerBtn = [[UIButton alloc] init];
    
    self.registerBtn = registerBtn;
    
    [registerBtn setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    
    [registerBtn addTarget:self action:@selector(registerBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:registerBtn];
    
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(smallBtn.mas_bottom).offset(30);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@40);
    }];
    
}

//获取验证码按钮
- (void)buttonClick:(UIButton *)button
{
    button.userInteractionEnabled = NO;
    
    //    NSLog(@"%@",self.phoneNumTextField.text);
    
    //    __block BOOL send = NO;
    
    if (self.phoneNumTextField.text.length == 11)
    {
        // 获取验证码 getVerificationCodeBySMSWithPhone
        [SMS_SDK getVerificationCodeBySMSWithPhone:self.phoneNumTextField.text zone:@"86" result:^(SMS_SDKError *error) {
            if (error) {
                
                [SVProgressHUD showErrorWithStatus:@"获取验证码失败"];
                
                [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
                
                [SVProgressHUD setFadeInAnimationDuration:0.5];
                
                [SVProgressHUD setFadeOutAnimationDuration:0.5];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                    
                    
                });
                
            } else {
                
                //                send = YES;
                
                [SVProgressHUD showSuccessWithStatus:@"发送成功"];
                
                [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
                
                [SVProgressHUD setFadeInAnimationDuration:0.5];
                
                [SVProgressHUD setFadeOutAnimationDuration:0.5];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                });
                
            }
        }];
    }
    else
    {
        [SVProgressHUD showInfoWithStatus:@"请输入正确的手机号"];
        
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        
        [SVProgressHUD setFadeInAnimationDuration:0.5];
        
        [SVProgressHUD setFadeOutAnimationDuration:0.5];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    }
    
    //    if (send)
    //    {
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    [queue addOperationWithBlock:^{
        
        int i = 10 ;
        
        while (i > 0) {
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                
                [button setTitle:[NSString stringWithFormat:@"%d秒",i] forState:UIControlStateNormal];
                
            }];
            
            [NSThread sleepForTimeInterval:1];
            
            i--;
        }
        
        if (i == 0)
        {
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [button setTitle:@"获取验证码" forState:UIControlStateNormal];
                button.userInteractionEnabled = YES;
            }];
            
        }
        
    }];
    //    }
    
}

//给注册按钮留的方法
- (void)registerBtnClick
{
    // 提交验证码(验证验证码) commitVerifyCode
    // 输入错误 已经验证过, 都不会成功
    [SMS_SDK commitVerifyCode:self.codeTextField.text result:^(enum SMS_ResponseState state) {
        switch (state) {
            case SMS_ResponseStateSuccess:
                [SVProgressHUD showSuccessWithStatus:@"验证成功"];
                
                [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
                
                [SVProgressHUD setFadeInAnimationDuration:0.5];
                
                [SVProgressHUD setFadeOutAnimationDuration:0.5];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                    
                });
                break;
            case SMS_ResponseStateFail:
                [SVProgressHUD showErrorWithStatus:@"验证失败"];
                
                [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
                
                [SVProgressHUD setFadeInAnimationDuration:0.5];
                
                [SVProgressHUD setFadeOutAnimationDuration:0.5];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                });
                break;
                
            default:
                break;
        }
    }];
}

//选择按钮被点击
- (void)smallBtnClick:(UIButton *)button
{
    if (!button.isSelected)
    {
        [button setSelected:YES];
    }else
    {
        [button setSelected:NO];
    }
    
}

#pragma mark -- 键盘弹出更改frame

- (void)changeKeyboard:(NSNotification *)notification
{
    //        NSLog(@"%@",notification);
    
    self.notification = notification;
    
    //1. 计算要移动的坐标
    
    //1.1 从通知中获取userInfo额外信息
    NSDictionary *userInfo = notification.userInfo;
    
    //1.2 获取开始的frame
    //    CGRect beginFrame = [userInfo[@"UIKeyboardFrameBeginUserInfoKey"] CGRectValue];
    
    //1.3 获取结束的frame
    CGRect endFrame = [userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    
    if (endFrame.origin.y - 40 < CGRectGetMaxY(self.registerBtn.frame))
    {
        //1.4 计算要移动的距离
        //    CGFloat move = endFrame.origin.y - beginFrame.origin.y;
        //        CGFloat move = endFrame.origin.y - self.view.bounds.size.height;
        
        CGFloat move = (self.registerBtn.frame.origin.y - endFrame.origin.y) * 0.5;
        
        
        //2. 让界面跟着移动
        self.view.transform = CGAffineTransformMakeTranslation(0, move);
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    
    if (self.view.frame.origin.y != 0)
    {
        [UIView animateWithDuration:0.25 animations:^{
            self.view.transform = CGAffineTransformIdentity;
        }];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
